package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.constants.Constants;
import com.easychat.entity.dto.TokenUserInfoDTO;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.UserInfoVO;
import com.easychat.service.UserInfoService;
import com.easychat.utils.StringTools;
import com.easychat.websocket.ChannelContextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.io.IOException;

@RestController("userInfoController")
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController{
    //TODO DONE
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private ChannelContextUtils channelContextUtils;

    /**
     * 获取当前登录用户的信息
     * @param request
     * @return
     */
    @RequestMapping("/getUserInfo")
    @GlobalInterceptor
    public ResponseVO getUserInfo(HttpServletRequest request) {
        TokenUserInfoDTO tokenUserInfoDTO = getTokenUserInfoDTO(request);
        UserInfo userInfo = userInfoService.getUserInfoByUserId(tokenUserInfoDTO.getUserId());
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo, userInfoVO);
        userInfoVO.setAdmin(tokenUserInfoDTO.getAdmin());
        return getSuccessResponseVO(userInfoVO);
    }

    /**
     * 保存用户信息
     * @param request
     * @param userInfo 新的用户信息
     * @param avatarFile 头像
     * @param avatarCover 头像缩略图
     * @return
     */
    @RequestMapping("/saveUserInfo")
    @GlobalInterceptor
    public ResponseVO saveUserInfo(HttpServletRequest request, UserInfo userInfo, MultipartFile avatarFile, MultipartFile avatarCover) throws IOException {
        TokenUserInfoDTO tokenUserInfoDTO = getTokenUserInfoDTO(request);
        userInfo.setUserId(tokenUserInfoDTO.getUserId());//只能修改自己的用户数据
        userInfo.setPassword(null);//不能修改的数据要置空
        userInfo.setStatus(null);
        userInfo.setCreateTime(null);
        userInfo.setLastOffTime(null);
        userInfoService.updateUserInfo(userInfo, avatarFile, avatarCover);
        //保存后，返回最新信息
        return getUserInfo(request);
    }

    /**
     * 修改密码
     * @param request
     * @param password 修改后的密码
     * @return
     */
    @RequestMapping("/updatePassword")
    @GlobalInterceptor
    public ResponseVO updatePassword(HttpServletRequest request,
                                     @NotEmpty @Pattern(regexp = Constants.REGEX_PASSWORD) String password) {
        TokenUserInfoDTO tokenUserInfoDTO = getTokenUserInfoDTO(request);
        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(StringTools.encodeMD5(password));//别忘了md5
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfoDTO.getUserId());
        //强制退出，重新登陆
        channelContextUtils.closeContext(tokenUserInfoDTO.getUserId());
        return getSuccessResponseVO(null);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    @GlobalInterceptor
    public ResponseVO logout(HttpServletRequest request) {
        TokenUserInfoDTO tokenUserInfoDTO = getTokenUserInfoDTO(request);
        channelContextUtils.closeContext(tokenUserInfoDTO.getUserId());
        return getSuccessResponseVO(null);
    }
}
